package com.wang.tool.monitor.callback;

import com.wang.tool.monitor.LinkLogUtil;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.util.SimpleLogUtil;

/**
 * Monitor全局回调
 * @author shaopeng
 * @date 2023/3/12
 */
public class GlobalMonitorCallback {


    private static MonitorCallback monitorCallback;


    /**
     * 后回调,write之前执行
     * @param monitorInfo 非空
     */
    public static void beforeWrite(MonitorInfo<?> monitorInfo) {
        if (monitorCallback == null) {
            return;
        }
        try {
            monitorCallback.beforeWrite(monitorInfo);
        } catch (Throwable e) {
            LinkLogUtil.newTagLog(MonitorInfo.TAG_MONITOR_ERROR)
                    .v1(monitorInfo.getBiz() + ":" + monitorInfo.getAction())
                    .v2("monitorBeforeWrite")
                    .addAttachment(MonitorInfo.ERROR, e)
                    .pointLink(monitorInfo.getLink())
                    .log();
        }
    }


    /**
     * 设置Monitor回调
     *
     * @param monitorCallback
     */
    public static void setCallback(MonitorCallback monitorCallback) {
        GlobalMonitorCallback.monitorCallback = monitorCallback;
        SimpleLogUtil.info("[changeGlobalMonitorCallback] monitorCallback:{0}", monitorCallback);
    }
}
